package com.exam.service.impl;


import com.exam.mapper.CityMapper;
import com.exam.pojo.CityDTO;
import com.exam.pojo.Citys;
import com.exam.pojo.RiskTop;
import com.exam.pojo.YearDataVo;
import com.exam.service.CityService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CityServiceimpl implements CityService {

    @Resource
    private CityMapper cityMapper;

    @Override
    public List<YearDataVo> getStat() {
        //获取表中现有年份
        List<Integer> years = cityMapper.getYears();

        if (years.size()==0){
            return null;
        }
        List<YearDataVo> list = new ArrayList<>();
        for (Integer year : years) {
            YearDataVo yearData = new YearDataVo();
            yearData.setYear(year);
            List<RiskTop> riskTops = cityMapper.getRiskTop(year);
            yearData.setRiskTop(riskTops);
            list.add(yearData);
        }
        return list;
    }

    @Override
    public Integer addCrime(CityDTO cityDTO) {
        Citys city = new Citys();
        BeanUtils.copyProperties(cityDTO,city);
        int insert = cityMapper.insert(city);
        Integer incidentId = null;
        if (insert == 1){
            incidentId = cityMapper.selectNewCity();
        }
        return incidentId;
    }
}
